package com.example.provider.controller;

import com.example.common.entity.Company;
import com.example.common.entity.Demand;
import com.example.common.entity.Message;
import com.example.common.entity.WorkRoom;
import com.example.provider.service.CompanyService;
import com.fasterxml.jackson.databind.ObjectMapper;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.io.IOException;
import java.util.List;
import java.util.Map;

@Api(value = "企业controller", tags = {"公司相关接口"})
@Slf4j
@RestController
@RequestMapping("/company")
public class CompanyController {
    @Autowired
    private CompanyService companyService;

    /**
     * 公司注册
     * @param company
     * @return
     * @throws Exception
     */
    @ResponseBody
    @CrossOrigin(origins = "*",maxAge = 3600)
    @PostMapping("/companyApply")
    public Message companyApply(@RequestBody Company company){
        return companyService.applyCompany(company);
    }

    /**
     * 发布需求
     */
    @ResponseBody
    @PostMapping("/releaseReq")
    public Message releaseReq(@RequestBody Demand demand) throws IOException {
        return companyService.releaseReq(demand);
    }

    /**
     * 删除需求,要同时把竞标关系中相关的也删掉
     * @param demand 要删除的需求
     * @return
     */
    @ResponseBody
    @PostMapping("/deleteDemand")
    public Message deleteDemand(@RequestBody Demand demand){
        return companyService.deleteDemand(demand);
    }

    /**
     * 根据当前用户id搜索公司
     * @param idd 当前登录用户id
     * @return
     */
    @GetMapping("/searchByIdd")
    public Company searchByIdd(int idd){
        return companyService.searchByIdd(idd);
    }

    /**
     * 根据公司id搜索对应的招标情况
     * @param id 当前登录用户id
     * @return
     */
    @GetMapping("/checkBiddingByCompany")
    public List<Demand> checkBiddingByCompany(int id) throws IOException {
        return companyService.checkBiddingByCompany(companyService.searchByIdd(id).getId());
    }

    /**
     * 根据需求id搜索对应的竞标工作室排名
     * @param demandId 需求id
     * @return
     */
    @GetMapping("/searchWorkroomByDemandId")
    public List<WorkRoom> searchWorkroomByDemandId(int demandId){
        return companyService.searchWorkroomByDemandId(demandId);
    }
}
